<?php


namespace App\Models\StoreCenter\Models;


use App\Models\StoreCenter\StoreCenterModel;
use Illuminate\Support\Facades\Cache;

class CouponModel extends StoreCenterModel
{
    //条件 1=无条件 2=满减 3=满折扣
    const NOT_CONDITION = 1;
    const NUMERIC_CONDITION = 2;
    const PERCENT_CONDITION = 3;

    const CACHE = 'xin:store:coupon:';
    const USED_CACHE = 'xin:store:coupon:used_count:';
    const TOTAL_CACHE = 'xin:store:coupon:total:';

    public $table = 'coupon';
    public $fillable = [
        'code', //varchar(16) NOT NULL DEFAULT '' COMMENT '编码',
        'title', //varchar(64) NOT NULL DEFAULT '' COMMENT '标题',
        'type', //tinyint(1) NOT NULL DEFAULT '1' COMMENT '类型 1=平台 2=商家',
        'store_id', //店铺id,
        'condition', //tinyint(1) NOT NULL DEFAULT '2' COMMENT '条件 1=无条件 2=满减 3=满折扣',
        'least_fee', //decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '条件金额',
        'purchase_num', //int NOT NULL DEFAULT '0' COMMENT '条件购买数量',
        'discount_fee', //decimal(10,2) NOT NULL COMMENT '折扣力度',
        'used_count', //int NOT NULL COMMENT '限制次数',
        'total', //int NOT NULL COMMENT '总数量',
        'create_user', //varchar(64) NOT NULL DEFAULT '' COMMENT '创建者',
        'create_user_uuid', //varchar(64) NOT NULL DEFAULT '' COMMENT '创建者uuid',
//        'create_time', //datetime DEFAULT NULL COMMENT '创建时间',
//        'update_time', //datetime DEFAULT NULL COMMENT '更新时间',
        'start_time', //datetime DEFAULT NULL COMMENT '开始时间',
        'expire_time', //datetime DEFAULT NULL COMMENT '开始时间',
        'status'
    ];

    public function getConditionCnAttribute($key)
    {
        $res = [
            1 => '无条件',
            2 => '满减',
            3 => '满折扣',
        ];
        return $res[$key] ?? '未知';
    }

    public function couponUser()
    {
        return $this->hasMany(CouponUserModel::class, 'coupon_code', 'code');
    }

    public static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $expire = is_null($model->expire_time) ? null : time() - strtotime($model->expire_time);
            $model->used_count !== 0 && Cache::set(self::USED_CACHE.$model->code, $model->used_count, $expire);
            $model->total !== 0 && Cache::set(self::TOTAL_CACHE.$model->code, $model->total, $expire);
        });

        static::updated(function ($model) {
            Cache::delete(self::CACHE.'*');
            Cache::delete(self::USED_CACHE.$model->code);
            Cache::delete(self::TOTAL_CACHE.$model->code);
        });

        static::deleted(function ($model) {
            Cache::delete(self::CACHE.'*');
            Cache::delete(self::USED_CACHE.$model->code);
            Cache::delete(self::TOTAL_CACHE.$model->code);
        });
    }

    public function decrCount($type, $coupon, $value = 1)
    {
        switch ($type) {
            case 1:
                Cache::decrement(self::USED_CACHE.$coupon, $value);
                break;
            case 2:
                Cache::decrement(self::TOTAL_CACHE.$coupon, $value);
                break;
            default:break;
        }
    }
}
